package top.lidee.module.backendApi.twice.management.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.lidee.common.dto.AccountInfo;
import top.lidee.common.service.twice.management.BmsManagerService;
import top.lidee.common.util.TokenUtil;
import top.lidee.framework.enums.CommonExceptionEnum;
import top.lidee.framework.result.PageUtils;
import top.lidee.framework.result.ResponseResult;
import top.lidee.framework.web.BaseController;
import top.lidee.repository.model.twice.vo.management.ManagerAddVo;
import top.lidee.utils.IPUtils;
import top.lidee.utils.IpUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 后台管理系统-管理员/代理商
 *
 * @author zhangzhan
 * @email 52194440@qq.com
 * @date 2024-09-23 14:39:33
 */
@Api("后台管理系统-管理员/代理商")
@RestController
@RequestMapping("/backendApi/bmsmanager")
public class BmsManagerController extends BaseController {

    @Autowired
    private BmsManagerService bmsManagerService;

    /**
     * 条件查询
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "后台管理系统-人员表列表")
    public ResponseResult list(HttpServletRequest request,@RequestBody Map<String, Object> params) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }


        String ipAddr = IPUtils.getIpAddr(request);
        PageUtils page = bmsManagerService.queryPage(params,accountInfo,ipAddr);


        return ResponseResult.okResultPage("page", page);
    }

    /**
     * 人员表新增下级
     */
    @RequestMapping(value = "/saveManager", method = RequestMethod.POST)
    @ApiOperation(value = "后台管理系统-人员表新增下级")
    public ResponseResult saveManager(HttpServletRequest request, @RequestBody @Validated ManagerAddVo managerAddVo) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult =bmsManagerService.saveManager(managerAddVo,accountInfo);

        return responseResult;
    }


    /**
     * 修改状态
     */
    @RequestMapping(value = "/updateStatus/{managerId}", method = RequestMethod.PUT)
    @ApiOperation(value = "后台管理系统-修改状态")
    public ResponseResult updateStatus(HttpServletRequest request,@PathVariable("managerId") Integer managerId) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult= bmsManagerService.updateStatus(managerId,accountInfo);

        return responseResult;
    }

    /**
     * 删除下级
     */
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.DELETE)
    @ApiOperation(value = "后台管理系统-人员表删除")
    public ResponseResult delete(HttpServletRequest request,@RequestBody Integer[] managerIds) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult= bmsManagerService.removeByIds(managerIds,accountInfo);

        return responseResult;
    }

}
